package edu.scau.cxq.gp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.common.utils.WrapperUtils;
import edu.scau.cxq.gp.dto.SupplierSignDTO;
import edu.scau.cxq.gp.entity.Supplier;
import edu.scau.cxq.gp.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 存储供应商信息 前端控制器
 * </p>
 *
 * @author CXQ
 * @since 2022-01-30
 */
@RestController
@ResponseResult
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    /**
     * 插入新供应商
     * @param supplier
     * @return
     */
    @PostMapping("/insertOne")
    public boolean insertOne(@RequestBody Supplier supplier) {

        return supplierService.save(supplier);
    }

    /**
     * 查找供应商
     * @return
     */
    @GetMapping("/select")
    public List<Supplier> select(Supplier supplier) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(supplier);
        queryWrapper.eq("sup_hidden", 0);
        List<Supplier> suppliers = supplierService.list(queryWrapper);
        return suppliers;
    }

    /**
     * 查找单个供应商
     */
    @GetMapping("/selectOne")
    public Supplier selectOne (Long supId) {
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sup_id", supId);
        Supplier supplier = supplierService.getOne(queryWrapper);

        return supplier;
    }

    /**
     * 分页查找供应商
     * @return
     */
    @GetMapping("/selectByPage")
    public IPage<Supplier> selectByPage (Integer currentPage, Integer size, Supplier supplier) {
        Page<Supplier> page = new Page<>(currentPage, size);
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(supplier);
        queryWrapper.eq("sup_hidden", 0);
        return supplierService.page(page, queryWrapper);
    }

    /**
     * 删除（实际上是隐藏）
     * @param supplier
     * @return
     */
    @PostMapping("/deleteOne")
    public boolean deleteOne(@RequestBody Supplier supplier) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(supplier);
        Supplier sup = supplierService.getOne(queryWrapper);
        // 将隐藏位置1
        sup.setSupHidden(1);
        return supplierService.updateById(sup);
    }

    /**
     * 修改供应商
     * @RequestBody：
     * 作用：
     * 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     * @param supplier
     * @return
     */
    @PostMapping("/updateOne")
    public boolean updateOne (@RequestBody Supplier supplier) {
        return supplierService.updateById(supplier);
    }

    /**
     * 获取所有的供应商标签
     * @return
     */
    @GetMapping("/selectSupplierSigns")
    public List<SupplierSignDTO> selectSupplierSigns () {
        return supplierService.findSupplierSigns();
    }
}
